package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * OpenConf.java
 * <p>
 * 
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0
 * @Date Jan 8, 2010
 */
public class ServerCPU extends AbstractJDBCCollector {
    private static final Logger logger = Logger.getLogger(ServerCPU.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql1 = "select tl,ol from (select ol=count(*)  from master..sysengines) a," + " (select tl=value from master..sysconfigures where name = 'max online engines') b";
            String sql2 = "select cpubusy=( @@cpu_busy+@@io_busy+0.0 )/@@timeticks ";

            String[] rs = this.getStrings(serverName, sql1, sql2, ip);
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase server CPU", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql1, String sql2, String ip) throws CollectorException {
        String[] propValArr = new String[3];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql1);
            int tl = -1;
            int ol = -1;
            while (rst.next()) {
                tl = rst.getInt(1);
                ol = rst.getInt(2);
            }
            if (rst != null) {
                rst.close();
                rst = null;
            }
            rst = this.getCachedResultSet("sybase", ip, serverName, sql2);

            float cpuBusy = -1;
            while (rst.next()) {
                cpuBusy = rst.getFloat(1);
            }
            propValArr[0] = tl + "";
            propValArr[1] = ol + "";
            propValArr[2] = cpuBusy + "";
        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "配置引擎数", "在线引擎数", "CPU繁忙率" };
        return new String[] { "ConfigureEngines", "OnlineEngines", "CPUBusyRatio" };
    }
}
